import { useBusProduct } from '/@/stores/busGoods';
import { getBusAttributeDropdownF,BusAttributeDropdown } from '/@/api/main/busGoods';

const stores = useBusProduct();

// 获取物料属性
export const getProductAttrListFn = async (goodCode: any) => {
  const { data } = await getBusAttributeDropdownF({ p1: goodCode });
  const list = data.result ?? [];
  // console.log(list)
  if (list.length) {
    stores.productAttrList = list.map((item: {attr:any, value: any }) => ( { attr: item.attr,  value: item.value } ));
    // console.log(stores.productAttrList,'11111')
  }

}

export const getProductAttrLstFn = async (productCode: any) => {
  const { data } = await BusAttributeDropdown({ p1: productCode });
  const list = data.result ?? [];
  if (list.length) {
    stores.productAttrRowList = list.map((item: {label:any, value: any }) => ({label:item.label, value: item.value } ));
  }
  // console.log(stores.productAttrRowList,3333)
}

// 获取物料分类
// export const getProductCateLstFn = async (productCode: any) => {
//   const { data } = await getProductCateList({ p1: productCode });
//   const list = data.result ?? [];
//   if (list.length) {
//     stores.productCateRowList = list.map((item: { value: any; }) => ({ value: item.value }));
//   }
// }

// 物料属性
export const productAttributeEffect = () => {
  // 增加物料属性
  const addProductAttributeRow = () => stores.setProductAttrList({attr: null, value: null});

  // 删除物料属性
  const deleteProductAttributeRow = (k: number) => stores.delProductAttrList(k, 1);

  return {
    addProductAttributeRow,
    deleteProductAttributeRow
  }
}

// 物料分类
export const productAttrRowEffect = () => {
  // 增加物料分类
  const addProductAttrRow = () => stores.setProductAttrRowList({});

  // 删除物料分类
  const deleteProductAttrRow = (k: number) => stores.delProductAttrRowList(k, 1);

  return {
    addProductAttrRow,
    deleteProductAttrRow
  }
}